package com.qf.shop.order.config;

import com.qf.shop.common.constants.OrderConstatns;
import net.sf.jsqlparser.statement.select.Top;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class RabbitMQConfig {

    // 1.创建一个订单的交换机
    @Bean
    public TopicExchange orderExchange() {
        return new TopicExchange(OrderConstatns.ORDER_EXCHANGE, true, false);
    }

    // 订单的延时队列
    @Bean
    public Queue orderTtlQueue() {
        Map<String, Object> args = new HashMap<>();
        args.put("x-message-ttl", 1000 * 60 * 3); // 3m
        args.put("x-dead-letter-exchange", OrderConstatns.ORDER_DLE_EXCHANGE); // 死信交换机
        args.put("x-dead-letter-routing-key", "order.dle.ttl"); // 死信路由键
        return new Queue(OrderConstatns.ORDER_TTL_QUEUE, true, false, false, args);
    }

    @Bean
    public Binding orderTtlQueueBind() {
        return BindingBuilder.bind(orderTtlQueue()).to(orderExchange()).with("order.*");
    }

    @Bean
    public TopicExchange orderDleExchange() {
        return new TopicExchange(OrderConstatns.ORDER_DLE_EXCHANGE, true, false);
    }

    @Bean
    public Queue orderTtlDelQueue() {
        return new Queue(OrderConstatns.ORDER_TTL_DLE_QUEUE, true, false, false);
    }

    @Bean
    public Binding orderTtlDelQueueBind() {
        return BindingBuilder.bind(orderTtlDelQueue()).to(orderDleExchange()).with("order.dle.*");
    }

    @Bean
    public Queue stockTtlUnlockQueue(){
        Map<String, Object> args = new HashMap<>();
        args.put("x-message-ttl", 1000 * 60 * 4); // 4m
        args.put("x-dead-letter-exchange", OrderConstatns.ORDER_DLE_EXCHANGE); // 死信交换机
        args.put("x-dead-letter-routing-key", "stock.dle.ttl"); // 死信路由键
        return new Queue(OrderConstatns.STOCK_TTL_UNLOCK_QUEUE,true,false,false,args);
    }

    @Bean
    public Binding stockTtlUnlockQueueBind(){
        return BindingBuilder.bind(stockTtlUnlockQueue()).to(orderExchange()).with("order.*");
    }

    @Bean
    public Queue stockDleUnlockQueue(){
        return new Queue(OrderConstatns.STOCK_DLE_UNLOCK_QUEUE,true,false,false);
    }

    @Bean
    public Binding stockDleUnlockBind(){
        return BindingBuilder.bind(stockDleUnlockQueue()).to(orderDleExchange()).with("stock.dle.*");
    }

}
